﻿using System;

namespace ConsoleApp9
{
    public class ArrayList<T>
    {
        private T[] _entities = default;
       
        private int _index = -1;
       
        public ArrayList() : this(16)
        {

        }

        public int Count 
        {
            get 
            {
                return _index + 1;
            }
        }

        public ArrayList(int capacity)
        {
            _entities = new T[capacity];
        }

        public void Add(T item)
        {
            _index++;
            if (_index < _entities.Length)
            {
                _entities[_index] = item;
            }
            else
            {
                ReSize();
                _entities[_index] = item;
            }
        }


        private void ReSize()
        {
            var newSize = _entities.Length * 2;
            var newEntities = new T[newSize];
            for (int i = 0; i < _entities.Length; i++)
            {
                newEntities[i] = _entities[i];
            }
            GC.SuppressFinalize(newEntities);
            _entities = newEntities;
        }
    }
}
